<?php
namespace Addons\Events\Controller;
class IndexController extends \Addon {

	public function __construct() {
		parent::__construct();
	}

	public function index() {
		$sql = "store_id='$this->admin_store_id'";
		// dump( strpos($this->k, "搜"));
		/*******回复期数*****/
		if ($this->k = I("k", "", "urldecode")) {
			$sql .= "  and (title like '%$k%' or info like '%$k%')";
		}
		$order = "id desc";
		$topic_model = BoxModel("sns_topic");
		$data = BoxModel("addon_events")->get($sql, 10, $order);
		foreach ($data['list'] as $key => $value) {
			$data['list'][$key]['user_count'] = $topic_model->where(array('pid' => $value['id'], 'type' => 'events', 'is_best' => 1))->count();
		}
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->admin_display();
	}

	/****群组里的帖子**/
	function topic() {
		$pid = I('pid');
		$type = I('type');
		$where = "is_show =1 and type='$type'";
		if ($pid) {
			$where .= " and pid='$pid'";
		}
		if ($pid) {
			$this->groupData = BoxModel("addon_events")->load($pid);
			$this->pidName = $this->groupData['title'];
			$this->pid = $this->groupData['id'];
		}
		if ($this->kw = I('kw')) {
			$where .= " and title like '%$this->kw%'";
		}
		if ($orderBy = I('orderBy')) {
			if ($orderBy == "comment") {
				$orderBy = "comment_count desc";
			}
			if ($orderBy == "good") {
				$orderBy = "good_count desc";
			}
			if ($orderBy == "addtime") {
				$orderBy = "dateline desc";
			}

		}
		$this->type = I('type');
		$data = BoxModel("sns_topic")->get($where, 10, $orderBy);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->admin_display();
	}

	function member() {
		$pid = I('pid');
		$type = I('type');
		$where = "is_show =1 and type='$type' and is_best=1";
		if ($pid) {
			$where .= " and pid='$pid'";
		}
		if ($pid) {
			$this->groupData = BoxModel("addon_events")->load($pid);
			$this->pidName = $this->groupData['title'];
			$this->pid = $this->groupData['id'];
		}
		if ($this->kw = I('kw')) {
			$where .= " and title like '%$this->kw%'";
		}
		$this->type = I('type');
		$data = BoxModel("sns_topic")->get($where, 100);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->admin_display();
	}

	/******发布活动****/
	public function add() {
		$model = BoxModel("addon_events");
		$id = I("id");
		$this->data = $model->load($id);
		if (IS_POST) {
			$arr = $model->create();

			$info = $this->_upload("/group/");
			if (is_array($info)) {
				$arr['pic'] = $info['picname']['savepath'] . $info['picname']['savename'];
			}
			$arr['expire_date'] = strtotime($arr['expire_date']);
			if ($id) {
				$model->save($arr);
			} else {
				$arr['addtime'] = time();
				$arr['store_id'] = $this->admin_store_id;
				$id = $model->add($arr);
			}

			if ($id) {
				$this->jump("保存成功", addons_url('add', array('id' => $id)));
			}
		}
		$this->admin_display();
	}

}